﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BSCMFF.Core.DataAccessLayer.DataObjects;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Core.BusinessLayer
{
    public class CompanyInfoManager
    {
        private BSCEntities context;//= new BSCEntities();

        public CompanyInfo GetCompanyInfo()
        {
            context = new BSCEntities();

            var enums = (from e in context.Enumarations.Include("Enumeration_Type")
                         where e.Enumeration_Type.Name.StartsWith("Company")
                         select e);

            CompanyInfo ci = new CompanyInfo();

            foreach (BSCMFF.Core.DataAccessLayer.Enumaration e in enums)
            {
                switch (e.Enumeration_Type.Name)
                {
                    case "CompanyName": ci.CompanyName = e.Name; break;
                    case "CompanyAddress1": ci.CompanyAddress1 = e.Name; break;
                    case "CompanyAddress2": ci.CompanyAddress2 = e.Name; break;
                    case "CompanyCity": ci.CompanyCity = e.Name; break;
                    case "CompanyZIP": ci.CompanyZip = e.Name; break;
                    case "CompanyCountry": ci.CompanyCountry = e.Name; break;
                    case "CompanyPhone": ci.CompanyPhone = e.Name; break;
                    case "CompanyURL": ci.CompanyURL = e.Name; break;
                    case "CompanyEmail": ci.CompanyEmail = e.Name; break;
                    case "CompanyLogo": ci.CompanyLogo = e.Image; break;

                    default: break;
                }
            }
            
            return ci;
        }

        private bool isCompanyName = false;
        private bool isCompanyAddress1 = false;
        private bool isCompanyAddress2 = false;
        private bool isCompanyCity = false;
        private bool isCompanyZIP = false;
        private bool isCompanyCountry = false;
        private bool isCompanyPhone = false;
        private bool isCompanyURL = false;
        private bool isCompanyEmail = false;
        private bool isCompanyLogo = false;

        public bool UpdateCompanyInfo(CompanyInfo ci)
        {
            BSCEntities context = new BSCEntities();

            var enums = (from e in context.Enumarations.Include("Enumeration_Type")
                         where e.Enumeration_Type.Name.StartsWith("Company")
                         select e);

            

            foreach (BSCMFF.Core.DataAccessLayer.Enumaration e in enums)
            {
                switch (e.Enumeration_Type.Name)
                {
                    case "CompanyName": e.Name = ci.CompanyName; isCompanyName = true; break;
                    case "CompanyAddress1": e.Name = ci.CompanyAddress1; isCompanyAddress1 = true; break;
                    case "CompanyAddress2": e.Name = ci.CompanyAddress2; isCompanyAddress2 = true; break;
                    case "CompanyCity": e.Name = ci.CompanyCity; isCompanyCity = true; break;
                    case "CompanyZIP": e.Name = ci.CompanyZip; isCompanyZIP = true; break;
                    case "CompanyCountry": e.Name = ci.CompanyCountry; isCompanyCountry = true; break;
                    case "CompanyPhone": e.Name = ci.CompanyPhone; isCompanyPhone = true; break;
                    case "CompanyURL": e.Name = ci.CompanyURL; isCompanyURL = true; break;
                    case "CompanyEmail": e.Name = ci.CompanyEmail; isCompanyEmail = true; break;
                    case "CompanyLogo": e.Image = ci.CompanyLogo; isCompanyLogo = true; break;
                    default: break;
                }
            }

            CreateEnumerations(ci, context);

            try
            {
                context.SaveChanges();
            }
            catch
            {
                return false; //throw new Exception();
            }

            return true;
        }

        private void CreateEnumerations(CompanyInfo ci, BSCEntities context)
        {
            

            List<Enumeration_Type> enumTypes = (from et in context.Enumeration_Type
                                                select et).ToList();

            if( isCompanyName == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyName;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                                            {
                                                return et.Name == "CompanyName";
                                            }
                                           ).ID;
                context.AddToEnumarations(e);
            }
            if( isCompanyAddress1 == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyAddress1;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyAddress1";
                }
                ).ID;
                context.AddToEnumarations(e);
            }

            if (isCompanyAddress2 == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyAddress2;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyAddress2";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyCity == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyCity;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyCity";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyZIP == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyZip;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyZIP";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyCountry == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyCountry;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyCountry";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyPhone == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyPhone;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyPhone";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyURL == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyURL;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyURL";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyEmail == false)
            {
                Enumaration e = new Enumaration();
                e.Name = ci.CompanyEmail;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyEmail";
                }
                ).ID;
                context.AddToEnumarations(e);
            }
            if (isCompanyLogo == false)
            {
                Enumaration e = new Enumaration();
                e.Image = ci.CompanyLogo;
                e.Type_FK = enumTypes.Find(delegate(Enumeration_Type et)
                {
                    return et.Name == "CompanyLogo";
                }
                ).ID;
                context.AddToEnumarations(e);
            }

            //context.SaveChanges();

        }
    }
}
